package com.geqian.web.serializer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.LongNode;

/**
 * @author geqian
 * @date 20:01 2024/1/10
 */
public class ListDeserializationConverter implements DeserializationConverter {

    @Override
    public Object conversion(TreeNode treeNode, BeanProperty beanProperty, ObjectMapper objectMapper) throws JsonProcessingException {
        ArrayNode arrayNode = (ArrayNode) treeNode;
        for (int i = 0; i < arrayNode.size(); i++) {
            arrayNode.set(i, new LongNode(arrayNode.get(i).asLong()));
        }
        return objectMapper.treeToValue(arrayNode,beanProperty.getType().getRawClass());
    }
}
